import { Button } from 'antd/es/radio';
import React, { useCallback ,useState} from 'react';
let pre
const UseCallback = () => {
    let [num, setNum] = useState(0)
    // 缓存函数本身,render的时候不用重新生成，适用于给子组件传递函数
    const func = useCallback(() => {
        alert(1)
    }, [])
    // const func =()=>{
    //     alert(1)
    // }
    if(!pre){
        pre = func
    }else{
        console.log(pre === func)
    }
    return (
        <div>
            {num} <Button type="primary" onClick={() => setNum(num + 1)}>+1</Button>
            <Child func={func} />
        </div>
    );
};


const Child = ({ func }) => {
    return (
        <div>
            子组件
            <Button onClick={func}>按钮</Button>
        </div>
    );
}



export default UseCallback;
